"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，shell命令验证
Create At   : 2024/10
Owner       : haomeng
Description :
    1、进入交互模式，执行cd/pwd命令
    2、进入交互模式，执行不存在的命令
Expect      :
    1、命令执行成功
    2、合理报错
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')

    def test_dsscmd(self):
        text = '-----step1:进入交互式模式，执行cd/pwd命令   expect:命令执行成功-----'
        self.log.info(text)

        cmd = f'''dsscmd -i'''
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "cd +data\\n"
                     expect "dsscmd>"
                     send "cd base\\n"
                     expect "dsscmd>"
                     send "pwd\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd1)
        res1 = self.node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('+data/base', res1.splitlines()[-2], '命令执行失败')
        self.assertNotIn('can not find', res1, '执行命令存在异常')

        text = '-----step2:进入交互式模式，   expect:查看成功-----'
        self.log.info(text)


        text = '-----step3:进入交互式模式，   expect:查看成功-----'
        self.log.info(text)


    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
